package cn.usian.service.impl;

import cn.usian.common.R;
import cn.usian.dao.EmployeeMapper;
import cn.usian.exception.NameExistsException;
import cn.usian.pojo.Employee;
import cn.usian.service.EmployeeService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;


@Service
@Transactional
public class EmployeeServiceImpl implements EmployeeService {

    @Resource
    private EmployeeMapper employeeMapper;

    @Override
    public R login(Employee employee) {
        //创建条件对象
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Employee::getUsername,employee.getUsername());
        //1.根据用户名查看用户名是否存在
        Employee one = employeeMapper.selectOne(queryWrapper);
        if (one==null){
            return R.error("用户不存在");
        }
        //2.如果用户信息存在,判定密码  把用户输入的密码通过md5加密之后跟数据库中的密码进行对比
        //DigestUtils.md5DigestAsHex(employee.getPassword().getBytes()
        if(!DigestUtils.md5DigestAsHex(employee.getPassword().getBytes()).equals(one.getPassword())){
            return R.success("密码不正确");
        }
        //3.用户密码确定，判定用户状态
        if (one.getStatus()!=1){
            return R.error("账户被冻结");
        }
        return R.success(one);
    }

    @Override
    public R findPage(Integer page,Integer pageSize,String name) {
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(name!=null,Employee::getName,name);
        Page<Employee> page1 = new Page<>(page,pageSize);
        Page<Employee> pageInfo = employeeMapper.selectPage(page1, queryWrapper);
        return R.success(pageInfo);
    }


    @Override
    public R add(Employee employee) {
        //当employee中的数据生成存在所需要的用户名存在异常
        LambdaQueryWrapper<Employee> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Employee::getUsername,employee.getUsername());
        Employee one = employeeMapper.selectOne(lambdaQueryWrapper);
        if(one!=null){
            //说明用户名存在 throw爆发异常
            throw  new NameExistsException(employee.getUsername());
        }
        //密码设置默认值
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        //创建时间
        employee.setCreateTime(LocalDateTime.now());
        employee.setUpdateTime(LocalDateTime.now());
        int i = employeeMapper.insert(employee);
        if (i>0){
            return R.success("添加成功");
        }
        return R.error("添加失败");
    }

    @Override
    public R updateStatus(Employee employee) {
//        LambdaUpdateWrapper<Employee> updateWrapper = new LambdaUpdateWrapper<>();
//        updateWrapper.set(Employee::getStatus,employee.getStatus());
//        updateWrapper.set(Employee::getId,employee.getId());
        employeeMapper.updateById(employee);
        return R.success("修改成功");
    }

    @Override
    public R findById(Long id) {
        return R.success(employeeMapper.selectById(id));
    }


}
